package cn.midas.basic.service.impl;

import cn.midas.basic.service.IGraphCodeService;
import cn.midas.basic.utils.StrUtils;
import cn.midas.basic.utils.VerifyCodeUtils;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class GraphCodeServiceImpl implements IGraphCodeService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String graph(String imageCodeKey) {
        // 判断key值是否为空 为空抛出自定义异常
            if(StringUtils.isEmpty(imageCodeKey)){
                throw new RuntimeException("参数值为空,请检查");
            }
        // 调用字符串工具类 获取四位随机字符
        String code = StrUtils.getComplexRandomString(4);
        // 把key 和随机四位字符 设置到redis中
        redisTemplate.opsForValue().set(imageCodeKey,code,5, TimeUnit.MINUTES);
        // 将code 值变成图形并编码base64
        String base64Str = VerifyCodeUtils.VerifyCode(100, 35, code);
        // 返回图形base64码
        return base64Str;
    }
}
